<?
if(!defined('InStern')) exit;
/*
Script zum Anzeigen einer Topic
Written by Sternen-Schlacht
*/

include("conf/forum.conf.php");
$seite=secstr($_GET[seite],5);
echo "<link href=\"$template/css/forum.css\" rel=\"stylesheet\" type=\"text/css\" />";
//Laden der Topic
if($topic=$db->fetch_assoc($db->query("SELECT * FROM topics WHERE Id='".secstr($_REQUEST[topic])."';")))
{
  //Laden des Forums
  if($forum=$db->fetch_assoc($db->query("SELECT * FROM foren WHERE Id='$topic[Forum]';")) AND $_SESSION[user][Admin]>=$topic[AllowRead]) 
  {
    //NEUEN BEITRAG ERSTELLEN
    if($_POST['do']=="newpost" AND $_SESSION[user][Admin]>=$topic[AllowWrite] AND strlen($_POST[text])>5)
    {
      if($_SESSION[lastPost]+$config_forum[spamschutz]<date(U))
      {
        $db->query("INSERT INTO posts(Topic, AutorId, Zeit, Text) VALUES(".secstr($_POST[topic],5).",".$_SESSION[user][Id].",".date(U).",'".secstr($_POST[text],0)."');")or die($db->query());
        //Zï¿½hler updaten
        $db->query("UPDATE user SET Profil_Beitraege=Profil_Beitraege+1, Profil_Zeichen=Profil_Zeichen+".strlen($_POST[text])." WHERE Id=".$_SESSION[user][Id].";");
        $db->query("UPDATE foren SET Last=".date(U).", Beitraege=Beitraege+1 WHERE Id='$forum[Id]'");
        $db->query("UPDATE topics SET Last=".date(U).", Beitraege=Beitraege+1 WHERE Id='$topic[Id]'");
        $_SESSION[lastPost]=date(U);
        echo "<script type=\"text/javascript\">location.href='$_SERVER[PHP_SELF]?page=viewtopic&topic=".secstr($_POST[topic],5)."&post=last';</script>";
        exit;
      }
      else
      {
        $textfeld_value=$_POST[text];
        echo 
        "<script type=\"text/javascript\">alert('".sprintf($lang[viewtopic_spamschutz], $config_forum[spamschutz])."')</script>";
      }
    }
    //Springen zu einem Post, fals nötig
    if($_REQUEST[post]=="last")
    {
      //Nachsehen auf welcher Seite sich der Beitrag befindet
      $result=$db->query("SELECT ID FROM posts WHERE Topic=".secstr($_REQUEST[topic],5)." ORDER by ID desc");
      $anzahl=$db->num_rows($result);
      $seite=ceil($anzahl/$config_forum[topics])-1;
      $post=$db->result(0,Id);
    }
    elseif($_REQUEST[post]>0)
    {
      //Nachsehen auf welcher Seite sich der Beitrag befindet
      $anzahl=$db->num_rows($db->query("SELECT ID FROM posts WHERE Topic=".secstr($_REQUEST[topic],5)." AND Id<".secstr($_REQUEST[post],5)));
      if($anzahl==0) $anzahl=1;
      $seite=ceil($anzahl/$config_forum[topics])-1;
      $post=secstr($_REQUEST[post],5);
    }

    //Beitrag als gelesen markieren
	settype($_SESSION[ungelesen][$forum[Id]], "array");
    if($topic[Last]>$_SESSION[last] AND !in_array($topic[Id],$_SESSION[ungelesen][$forum[Id]])) $_SESSION[ungelesen][$forum[Id]][]=$topic[Id];
    if(!in_array($topic[Id],$_SESSION[gelesen]))
    {
      $_SESSION[gelesen][]=$topic[Id];
      $db->query("UPDATE topics SET Gelesen=Gelesen+1 WHERE Id=$topic[Id];");
    }
    //Adminrang laden----------------OHNEHIN EINLEITUNG DES ADMINISTRATIONSSYSTEMS
    include("sc/admincheck.sc.php"); 
    if($_REQUEST[close]=="yes" AND $admin)
    {
      $db->query("UPDATE topics SET Closed=1 WHERE Id=$topic[Id];") ;
      $topic[Closed]=1;
    }
    if($_GET['do']=="erasepost" AND $admin){
      $db->query("DELETE FROM posts WHERE Topic=$topic[Id] AND Id='".secstr($_GET[post],5)."';");
      $db->query("UPDATE topics SET Beitraege=Beitraege-1 WHERE Id=$topic[Id];");
      echo $lang[viewtopic_posterased];
    }
    if($_GET['moveto']>0 AND $admin){
        $f=$db->real_escape_string($_GET[moveto]);
        $db->query("UPDATE topics SET Forum=".intval($f)." WHERE Id=$topic[Id];");
        $db->query("UPDATE foren SET Themen = Themen - 1, Beitraege = Beitraege - $topic[Beitraege] WHERE Id=$topic[Forum];");
        $db->query("UPDATE foren SET Themen = Themen + 1, Beitraege = Beitraege + $topic[Beitraege] WHERE Id=".intval($f).";");        
        echo "<script type=\"text/javascript\">location.href='$_SERVER[PHP_SELF]?page=$_SESSION[page]&topic=$topic[Id]';</script>";
    }
    
    //Header ausgeben
    echo "<h1>$topic[Betreff]</h1>";
	if($topic[AllowRead]>0)
	{
	  foreach($config[adminrang] AS $key=>$value)
	    if($topic[AlloRead]<$key) $reader[]=$value."n";
	  echo "<div class=\"info\">".
	  sprintf($lang[viewtopic_notreadable], $reader)."
	  </div>";
	}
    echo "
    <table class=\"forum_options\" cellpadding=\"5\">
     <tr>
      <td>
       <input type=\"button\" value=\"$lang[viewtopic_reply]\" onclick=\"window.location.hash='#antwort';\"/>
      </td>
      <th width=\"90%\">
       <a href=\"$_SERVER[PHP_SELF]?page=forum\">$lang[viewtopic_forum]</a> ->
       <a href=\"$_SERVER[PHP_SELF]?page=topics&forum=$forum[Id]\">$forum[Name]</a> ->
       <a href=\"$_SERVER[PHP_SELF]?page=viewtopic&topic=$topic[Id]\">$topic[Betreff]</a>
      </th>
     </tr>
    </table>    ";
    echo "<table class=\"forum_post\">";
    $result=$db->query("SELECT * FROM posts WHERE Topic=$topic[Id] ORDER BY Id ASC LIMIT ".($seite*$config_forum[posts]).", ".$config_forum[posts].";");
    while($posts=$db->fetch_assoc($result))
    {
      //Markieren wenn Sein muss
     //Laden der Benutzerdaten
      if($user=$db->fetch_assoc($db->query("SELECT * FROM user WHERE Id='$posts[AutorId]';"))) $exists=true;
      else $exists=false;
      if(file_exists("data/user/user-$user[Id].jpg")) $bild="<img src=\"data/user/user-$user[Id].jpg\" width=\"100px\" height=\"100px\">";
      else $bild="";
      echo "
      <tr>
       <th colspan=\"2\" class=\"forum_post_head\"></th>
      </tr>
      <tr>
       <td class=\"forum_post_userinfo\" rowspan=\"2\">
        <div class=\"forum_post_username\" onclick=\"location.href='index.php?page=showuser&id=$user[Id]';\">
         $user[Name]
        </div>";
        if($bild!="")
          echo "
          <div class=\"forum_post_userbild\">
           $bild
          </div>";
        //Rang bestimmen
        if($user[Rang]>0){
            echo "[".$config[adminrang][$user[Rang]]."]<br/>";
        }else{   
            foreach($config_forum[rang] AS $key=>$value){
              $nextkey=$key;
              if($key>$user[Profil_Beitraege]) break;
              $rang=$value;
              $lastkey=$key;
            }
            //Next level       
            //echo "Next: $nextkey Last: $lastkey";
            $pro=100/($nextkey-$lastkey)*($user[Profil_Beitraege]-$lastkey);
            $next=round(120*$pro/100);
            echo "[$rang]<br>
            <div class=\"forum_post_spambar\">
             <div class=\"forum_post_spamstate\" style=\"width:".$next."px;\"></div>
            </div>";
        }
        echo "
        $lang[viewtopic_postcount]: $user[Profil_Beitraege]<br>
        $lang[viewtopic_charcount]: ".round($user[Profil_Zeichen]/$user[Profil_Beitraege]);
        echo "
       </td>
       <td class=\"forum_post_beitrag\">
         <div class=\"forum_post_beitrag_info\">		 
          $lang[viewtopic_cdate] ".date("d.m.Y",$posts[Zeit])." $lang[viewtopic_ctime] ".date("H:i:s",$posts[Zeit])."";
          echo "          
         </div>
         <div class=\"forum_post_beitrag_content\">
           <div class=\"forum_post_options\">";
           if($admin OR $user[Id]==$_SESSION[user][Id]) echo "<input type=\"button\" value=\"edit\" onclick=\"location.href='$_SERVER[PHP_SELF]?page=edittopic&post=$posts[Id]';\">";
           if($admin AND $posts[Zeit]!=$topic[Zeit]) echo "<input type=\"button\" value=\"X\" onclick=\"if(confirm('$lang[viewtopic_erasepost]')) location.href='$_SERVER[PHP_SELF]?page=$_SESSION[page]&topic=$topic[Id]&do=erasepost&post=$posts[Id]';\">";
           echo "
           </div>";

         $text=smallHTML($posts[Text]);
         if($_SESSION[marked]!="") $text=str_ireplace(secstr($_SESSION[marked]),"<span class=\"marked\">".secstr($_SESSION[marked])."</span>",$text);
         echo $text;
         echo "
         </div>
       </td>
      </tr>
      <tr>
       <td class=\"forum_post_signatur\">        
        $user[Profil_Signatur]
        <a name=\"#$posts[Id]\" id=\"post$posts[Id]\"></a>
       </td>
      </tr>
      <tr class=\"trenner\">
       <th colspan=\"2\"></th>
      </tr>     
      ";
    }
    echo "</table>
    <center>".
    leiste("$_SERVER[PHP_SELF]?page=$_SESSION[page]&topic=$topic[Id]",ceil(($topic[Beitraege]+1)/$config_forum[topics]),$seite);
    if($admin){ 
		if($topic[Closed]==0){
			echo "<br><input type=\"button\" value=\"Close topic\" onclick=\"if(confirm('close topic?')) location.href='$_SERVER[PHP_SELF]?page=$_SESSION[page]&topic=$topic[Id]&close=yes';\"/>";
		}
		
		//funktionality to move topics
		echo 
		"Move topic to: <select onchange=\"location.href='$_SERVER[PHP_SELF]?page=$_SESSION[page]&topic=$topic[Id]&moveto='+this.value;\">
			<option>- select target -</option>";
		$sql="SELECT Id, Name FROM foren;";
		$db->query($sql);
		while($row=$db->fetch_assoc()){
		    echo "<option value=\"$row[Id]\">$row[Name]</option>";
		}
		echo "
		</select>";
		     
	}
     echo "
    </center>";
    
    //Feld fï¿½r neuen Beitrag erstellen
    if($_SESSION[user][Rang]>=$topic[AllowWrite] AND $topic[Closed]==0)
    {
      echo "
      <h2>$lang[viewtopic_reply]</h2>
      <form action=\"$_SERVER[PHP_SELF]\" method=\"post\" onsubmit=\"setReady4Submit()\">
       <input type=\"hidden\" name=\"page\" value=\"$_SESSION[page]\"/>
       <input type=\"hidden\" name=\"topic\" value=\"$topic[Id]\"/>
       <input type=\"hidden\" name=\"do\" value=\"newpost\"/>
       <input type=\"hidden\" name=\"post\" value=\"last\" />";
       richeditor("text",$textfeld_value);
       echo "
       <input type=\"submit\" value=\"$lang[viewtopic_createreply]\"/>
       <a name=\"antwort\" id=\"antwort\">
      </form>";
    }
    //Automatisch zu einem bestimmten Beitrag springen
    if($post>0)
    {
      echo "
      <script type=\"text/javascript\">
       document.getElementById('post$post').focus();
     </script>";
    }
  }
  else echo $lang[viewtopic_topicnotfound];
}
else echo $lang[viewtopic_forumnotfound];

$db->query("UPDATE user SET Last_Login=".date(U)." WHERE Id=".$_SESSION[user][Id].";") ;
